﻿namespace WMS6.Web.CustomizeReport
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CustomizeReportServiceRef;
    using WMS6.Web.UserControls;

    public class LPSaleRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F16041;
        protected GridView gvSale;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        public void BindData2StockList(LPSaleReportQueryEntity lPSaleReportQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(lPSaleReportQueryEntity.CurrentPage);
            DataSet ds = CustomizeReportServiceClientProxy.GetInstance().GetLPSaleReport(lPSaleReportQueryEntity);
            if (ds.Tables.Count > 1)
            {
                this.CreateColumns(ds.Tables[2]);
            }
            else
            {
                this.CreateColumns(ds.Tables[0]);
            }
            base.BindData2GridView(this.gvSale, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            LPSaleReportQueryEntity lPSaleReportQueryEntity = this.GetLPSaleReportQueryEntity();
            lPSaleReportQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = lPSaleReportQueryEntity.CurrentPage;
            lPSaleReportQueryEntity.CurrentPage = "0";
            this.gvSale.Columns[1].Visible = false;
            this.gvSale.AllowSorting = false;
            this.gvSale.ShowFooter = false;
            try
            {
                this.BindData2StockList(lPSaleReportQueryEntity);
                base.Export2Excel("LPSaleReport.xls", this.gvSale);
            }
            finally
            {
                lPSaleReportQueryEntity.IsGetAll = false;
                lPSaleReportQueryEntity.CurrentPage = currentPageIndex;
                this.SetLPSaleReportQueryEntity(lPSaleReportQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            LPSaleReportQueryEntity lPSaleReportQueryEntity = this.GetLPSaleReportQueryEntity();
            lPSaleReportQueryEntity.CurrentPage = "0";
            this.BindData2StockList(lPSaleReportQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            LPSaleReportQueryEntity lPSaleReportQueryEntity = this.GetLPSaleReportQueryEntity();
            lPSaleReportQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2StockList(lPSaleReportQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            LPSaleReportQueryEntity lPSaleReportQueryEntity = this.GetLPSaleReportQueryEntity();
            lPSaleReportQueryEntity.OWNER_ID = "";
            lPSaleReportQueryEntity.SKU_ID = "";
            lPSaleReportQueryEntity.SKU_CLASS_ID = "";
            lPSaleReportQueryEntity.DESCR = "";
            lPSaleReportQueryEntity.BEGIN_DATE = DateTime.Today.ToString("yyyy-MM-dd");
            lPSaleReportQueryEntity.END_DATE = DateTime.Today.AddDays(1.0).ToString("yyyy-MM-dd");
            this.BindData2StockList(lPSaleReportQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            LPSaleReportQueryEntity lPSaleReportQueryEntity = this.GetLPSaleReportQueryEntity();
            if (int.Parse(lPSaleReportQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                lPSaleReportQueryEntity.CurrentPage = (int.Parse(lPSaleReportQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2StockList(lPSaleReportQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            LPSaleReportQueryEntity lPSaleReportQueryEntity = this.GetLPSaleReportQueryEntity();
            if (int.Parse(lPSaleReportQueryEntity.CurrentPage) > 0)
            {
                lPSaleReportQueryEntity.CurrentPage = (int.Parse(lPSaleReportQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2StockList(lPSaleReportQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            LPSaleReportQueryEntity lPSaleReportQueryEntity = this.GetLPSaleReportQueryEntity();
            this.BindData2StockList(lPSaleReportQueryEntity);
        }

        private void CreateColumns(DataTable dt)
        {
            int i;
            for (i = this.gvSale.Columns.Count - 1; i > 6; i--)
            {
                this.gvSale.Columns.RemoveAt(i);
            }
            int width = 700;
            for (i = 8; dt.Columns.Count > i; i++)
            {
                if (dt.Columns[i].ColumnName.ToUpper() != "ROWSTAT")
                {
                    BoundField bf = new BoundField {
                        HeaderText = dt.Columns[i].ColumnName,
                        DataField = dt.Columns[i].ToString(),
                        SortExpression = dt.Columns[i].ToString(),
                        DataFormatString = base.GetFormatDigitString()
                    };
                    bf.ItemStyle.CssClass = "GridViewRowWidth_100";
                    width += 100;
                    this.gvSale.Columns.Add(bf);
                    this.gvSale.Width = width;
                }
            }
        }

        private LPSaleReportQueryEntity GetLPSaleReportQueryEntity()
        {
            return (base.PageQueryEntitySession as LPSaleReportQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                LPSaleReportQueryEntity lPSaleReportQueryEntity = this.GetLPSaleReportQueryEntity();
                lPSaleReportQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2StockList(lPSaleReportQueryEntity);
            }
        }

        protected void gvSale_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F16041", this.gvSale);
        }

        protected void gvSale_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (this.hdnActionID.Value != "EXPORT")
                {
                    e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                    e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                    ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                    e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
            }
        }

        protected void gvSale_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                LPSaleReportQueryEntity lPSaleReportQueryEntity = this.GetLPSaleReportQueryEntity();
                lPSaleReportQueryEntity.SortField = e.SortExpression;
                if ((lPSaleReportQueryEntity.SortDirection == "ASC") || (lPSaleReportQueryEntity.SortDirection == ""))
                {
                    lPSaleReportQueryEntity.SortDirection = "DESC";
                }
                else if (lPSaleReportQueryEntity.SortDirection == "DESC")
                {
                    lPSaleReportQueryEntity.SortDirection = "ASC";
                }
                this.BindData2StockList(lPSaleReportQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            LPSaleReportQueryEntity lPSaleReportQueryEntity = new LPSaleReportQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    lPSaleReportQueryEntity.OWNER_ID = this.PreviousPage.OwnerID;
                    lPSaleReportQueryEntity.SKU_ID = this.PreviousPage.SkuID;
                    lPSaleReportQueryEntity.SKU_CLASS_ID = this.PreviousPage.SkuClassID;
                    lPSaleReportQueryEntity.DESCR = this.PreviousPage.Descr;
                    lPSaleReportQueryEntity.BEGIN_DATE = this.PreviousPage.BeginDate;
                    lPSaleReportQueryEntity.END_DATE = this.PreviousPage.EndDate;
                }
                else
                {
                    lPSaleReportQueryEntity.OWNER_ID = "";
                    lPSaleReportQueryEntity.SKU_ID = "";
                    lPSaleReportQueryEntity.SKU_CLASS_ID = "";
                    lPSaleReportQueryEntity.DESCR = "";
                    lPSaleReportQueryEntity.BEGIN_DATE = DateTime.Today.ToString("yyyy-MM-dd");
                    lPSaleReportQueryEntity.END_DATE = DateTime.Today.AddDays(1.0).ToString("yyyy-MM-dd");
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            lPSaleReportQueryEntity.IsGetAll = false;
            lPSaleReportQueryEntity.CurrentPage = "0";
            lPSaleReportQueryEntity.PageSize = this.gvSale.PageSize.ToString();
            lPSaleReportQueryEntity.SortField = "OWNER_ID,SKU_CLASS_ID,SKU_ID";
            lPSaleReportQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvSale.PageSize;
            this.SetLPSaleReportQueryEntity(lPSaleReportQueryEntity);
            this.BindData2StockList(lPSaleReportQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvSale);
                this.LoadData();
            }
        }

        public void SetLPSaleReportQueryEntity(LPSaleReportQueryEntity lPSaleReportQueryEntity)
        {
            base.PageQueryEntitySession = lPSaleReportQueryEntity;
        }

        public LPSaleRptQuery PreviousPage
        {
            get
            {
                return (LPSaleRptQuery) base.PreviousPage;
            }
        }
    }
}

